<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button class="btn" type="danger"
      plain round size="mini"
      @click="isEdit = !isEdit"
        >{{isEdit ? '完成': '编辑'}}</van-button
      >
    </van-cell>
    <van-grid class="wd-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channels, index) in mychannels"
        :key="index"
        @click="onMyChannelClick(channels, index)"
      >
              <van-icon
              v-show="isEdit && index!==0"
              slot="icon"
              name="clear"
              />
        <!-- 自定义插槽 -->
        <!-- v-bind: class 语法 -->
        <!--一个对象的key表示要作用的css类名， -->
        <!-- 对象中的value要计算出布尔值，true则作用该类名，false不作用类名 -->
        <span class="title" :class="{ active: index === active }" slot="text">
          {{ channels.name }}
        </span>
      </van-grid-item>
    </van-grid>
    <!-- 我的频道 -->

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title">频道推荐</div>
    </van-cell>
    <van-grid class="tj-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channels,index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channels.name"
        @click="onAddget(channels,index)"
      />
    </van-grid>
    <!-- 频道推荐 -->
  </div>
</template>

<script>
import {
  getAllchannel,
  addUserChannel,
  deleteUserChannel
} from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utlis/storage.js'
export default {
  // 计算属性
  computed: {

    // 计算属性会1观测内部依赖数据的变化
    // 如果依赖的数据发生变化，则计算属性会重新执行
    recommendChannels () {
      return this.allchannels.filter(channel => {
        const n = !this.mychannels.find(mychannels =>
          mychannels.id === channel.id)
        return n
      })
    },
    ...mapState(['user'])
  },
  data () {
    return {
      allchannels: [], // 所有频道
      isEdit: false, // 控制编辑状态的显示
      fiexChannels: [0] // 固定频道，不允许删除1
    }
  },
  created () {
    this.loadAllchannel()
  },
  name: 'ChannelEdit',
  props: {
    mychannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  methods: {
    async loadAllchannel () {
      try {
        const { data } = await getAllchannel()
        this.allchannels = data.data.channels
      } catch (err) {
        this.$toast('获取失败')
      }
    },

    // 点击添加频道
    async onAddget (channel) {
      // eslint-disable-next-line vue/no-mutating-props
      this.mychannels.push(channel)
      if (this.user) {
        try {
          await addUserChannel({
            id: channel.id, // 频道id
            seq: this.mychannels.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.mychannels)
      }
    },

    onMyChannelClick (channel, index) {
      if (this.fiexChannels.includes(channel.id)) {
        return
      }
      if (this.isEdit) {
        // 频道是否为推荐
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 删除频道
        // eslint-disable-next-line vue/no-mutating-props
        this.mychannels.splice(index, 1)

        // 处理持久化
        this.deleteChannel()
      } else {
        // 非编辑状态
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.mychannels)
        }
      } catch (err) {
        this.$tosat('操作失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {

  padding: 85px 0;

  .title-text {
    color: #333333;
    font-size: 32px;
  }

  .btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85985;
    border: 1px solid #f85985;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap; //文字不折行
      background-color: #f4f5f6;
      .van-grid-item__text, text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
    }
            .van-grid-item__icon-wrapper {
        position: unset;
      }
  }
  /deep/ .tj-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row; //水平排序
        .van-icon-plus {
          font-size: 28px;
        }
      }
    }
  }
}
/deep/ .wd-grid {
  .grid-item {
    .van-icon-clear {
      position: absolute;
      right: -5px;
      top: -5px;
      font-size: 15px;
      color: #cacaca;
      z-index: 2;
    }
  }
}
.active{
  color:red;
}

</style>
